package valid

import (
	"github.com/astaxie/beego/validation"
	"github.com/pkg/errors"
	"reflect"
	"strings"
)

type LoginCheck struct {
	Username string `alias:"用户名或手机号" valid:"Required; MaxSize(20)" form:"username"` // 用户名或手机号
	Password string `alias:"密码" valid:"Required; MaxSize(32)" form:"password"`      // 密码
	Code     string `alias:"验证码" valid:"MaxSize(4)" form:"code"`                    // 验证码
}

func (a *LoginCheck) ValidLogin() (err error) {
	InitValidate()
	valid := validation.Validation{}
	b, _ := valid.Valid(a)
	if !b {
		//表示获取验证的结构体
		st := reflect.TypeOf(LoginCheck{})
		for _, err := range valid.Errors {
			//获取验证的字段名和提示信息的别名
			filed, _ := st.FieldByName(err.Field)
			var alias = filed.Tag.Get("alias")
			var message = strings.Replace(err.Message, err.Field+" ", "", 1)
			//返回验证的错误信息
			return errors.New(alias + message)
		}
	}
	return nil
}
